package com.cyb.beta.controller;

import com.cyb.beta.core.Constant;
import com.cyb.beta.model.User;
import com.cyb.beta.service.RoleService;
import com.cyb.beta.service.UserService;
import com.jfinal.ext.route.ControllerBind;

/**
 * Created by Administrator on 2015/6/15.
 */
@ControllerBind(controllerKey = "/admin/user", viewPath = "admin/user")
public class UserController extends BaseController {

    private UserService userService = new UserService();
    private RoleService roleService = new RoleService();

    public void index() {
        keepModel(User.class);
        User user = getModel(User.class);
        int pageNumber = getParaToInt(Constant.PAGENUM, 1);
        int pageSize = getParaToInt(Constant.PAGESIZE, Constant.DEFAULTPAGESIZE);
        setAttr("pageList", userService.getUserPage(pageNumber, pageSize, user));
        render("userList.html");
    }

    /**
     * 查看用户
     */
    public void view() {
        Long id = getParaToLong(User.ID);

        setAttr("user", null != id ? User.dao.findById(id) : "");
        setAttr("roleStr", null != id ? userService.getUserRole(id) : "");

        setAttr("roleList", roleService.getRoleList());

        render("userForm.html");
    }

    /**
     * 修改或保存user
     */
    public void save() {
        User user = getModel(User.class);
        String[] roleIds = getParaValues("rids");

        userService.modifyUser(user, roleIds);
        redirect("/admin/user");
    }

    /**
     * 删除用户
     */
    public void delete() {
        String[] ids = getParaValues("ids");
        userService.deleteUsers(ids);
        redirect("/admin/user");
    }

}
